package com.jye.rseven.user.serivce.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.jye.rseven.domain.TyPtCode;
import com.jye.rseven.domain.TzPtCode;
import com.jye.rseven.domain.User;
import com.jye.rseven.service.TzPtCodeService;
import com.jye.rseven.user.mapper.TzPtCodeMapper;
import com.jye.rseven.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class TzPtCodeServiceImpl implements TzPtCodeService {

    @Autowired
    TzPtCodeMapper tzPtCodeMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public int addTz(TzPtCode tzPtCode) {
        try{
            Date date = new Date();
            tzPtCode.setAddTime(date);
            tzPtCode.setEndTime(new Date(date.getTime() + 24*60*60*1000));
            tzPtCodeMapper.addTz(tzPtCode);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public int updatePtStatus(int type2, String pid) {
        try{
            tzPtCodeMapper.updatePtStatus(type2,pid);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public List<TzPtCode> selectFutureOverTimeOrder() {
        return tzPtCodeMapper.selectFutureOverTimeOrder();
    }

    @Override
    public void updateCloseOverTimeOrder(Long dataId) {
        tzPtCodeMapper.updateCloseOverTimeOrder(dataId);
    }

    @Override
    public TzPtCode findTzByPtCode(String ptCode) {
        return tzPtCodeMapper.findTzByPtCode(ptCode);
    }

    @Override
    public TzPtCode findPtById(int id) {
        User u = userMapper.findUserById(tzPtCodeMapper.findUidById(id));
        TzPtCode tz = tzPtCodeMapper.findPtById(id);
        tz.setUser(u);
        return tz;
    }

    @Override
    public List<TzPtCode> findPtByUId(int uId, int skip, int limit) {
        User u = userMapper.findUserById(uId);
        List<TzPtCode> list = tzPtCodeMapper.findPtByUId(uId,skip,limit);
        for (TzPtCode tz: list){
            tz.setUser(u);
        }

        return list;
    }

    @Override
    public int findUidById(int id) {
        return tzPtCodeMapper.findUidById(id);
    }
}
